const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");


router.post('/scan/findById', async ctx => {
    let tenantId = ctx.user.tenantId;
    let scanInfo = ctx.request.body.scanInfo;
    let option = {type: QueryTypes.SELECT}
    let optionOne = {type: QueryTypes.SELECT, plain: true}
    if (!scanInfo) {
        return ctx.err('未找到数据');
    }
    let ret = scanInfo.split('-');
    let transType = tool.xe.toNumber(ret[0])
    let billId = tool.xe.toNumber(ret[1])
    let columns = [];
    // 出入库票据
    if ([150501,150502,150601,150602,150603,150706,150806].indexOf(transType) !== -1) {
        let attr = 'id,billType,transTypeName,buId,billDate,billState,accId,accId1,transType,billNo,leftAmount,hxAmount,amount,arrears,buName,linkName,address,salesId,linkMobile'
        let invoice = await db.query(`select ${attr} from v_invoice where tenantId=${tenantId} and id=${billId} `, optionOne)
        if (!invoice) {
            return ctx.err('未找到该票据');
        }
        if (invoice.transType === 150502 || invoice.transType === 150602) {
            invoice.leftAmount = -invoice.leftAmount
            invoice.hxAmount = -invoice.hxAmount
        }
        invoice.info = await db.query(`select * from v_main_info where iid=${billId} `, option);
        // 是否已审核，是就返回审核凭证     // 凭证号
        let cardNo = await db.query(`select billNo from card where tenantId=${tenantId} and byIid=${billId} `, optionOne)
        let sfkNo = await db.query(`select b.* from invoice a inner join invoice_receipt b on a.id=b.iid and a.tenantId=${tenantId} and b.billId=${billId} and a.billState='accounted'`)
        invoice.cardNo = cardNo?.billNo;
        let sfk =[]
        for (let item in sfkNo) {
            sfk.push(item.billNo);
        }
        invoice.sfkNo = sfk;
        if (invoice.billState === 'draft') {
            invoice.state = '草稿';
        }
        if (invoice.billState === 'accounted') {
            invoice.state = '已审核';
        }
        if (invoice.billState === 'del') {
            invoice.state = '已删除';
        }
        if (tool.xe.toNumber(invoice.leftAmount) === 0) {
            invoice.payState = '已核销';
        }
        if (tool.xe.toNumber(invoice.hxAmount) < tool.xe.toNumber(invoice.amount)) {
            invoice.payState = '部分核销';
        }
        if (tool.xe.toNumber(invoice.hxAmount) === 0) {
            invoice.payState = '未核销';
        }
        columns = [
            { type: 'seq', width: 50 },
            { field: 'invName', title: '商品名称', width: 220 },
            { field: 'unit', title: '单位', width: 60,align: 'center'},
            { field: 'qty', title: '数量', width: 100,align: 'right' },
            { field: 'price', title: '单价', width: 120,align: 'right',formatter: 'formatPrice'  },
            { field: 'deduction', title: '返现', width: 100,align: 'right',formatter: 'formatMoney' },
            { field: 'amount', title: '金额', width: 120,align: 'right',formatter: 'formatMoney'  },
            { field: 'send', title: '赠品', width: 200,align: 'center' },
            { field: 'remark', title: '备注'},
        ]
        invoice.columns = columns
        return ctx.suc('', invoice);
    }
    // 收付款票据
    if ([153001, 153101].indexOf(transType) !== -1) {
        let attr = 'id,billType,transTypeName,billState,transType,billNo,leftAmount,hxAmount,amount,arrears,buName,linkName,address,linkMobile,accName,accName1,accAmount,accAmount1'
        let invoice = await db.query(`select ${attr} from v_invoice where tenantId=${tenantId} and id=${billId}`, optionOne)
        if (!invoice) {
            return ctx.err('未找到该票据');
        }
        invoice.info = await db.query(`select * from invoice_receipt where iid=${billId}`, option);
        // 是否已审核，是就返回审核凭证     // 凭证号
        let cardNo = await db.query(`select billNo from card where tenantId=${tenantId} and byIid=${billId}`, optionOne)
        invoice.cardNo = cardNo?.billNo;
        if (invoice.billState === 'draft') {
            invoice.state = '草稿';
        }
        if (invoice.billState === 'accounted') {
            invoice.state = '已审核';
        }
        if (invoice.billState === 'del') {
            invoice.state = '已删除';
        }
        columns = [
            { type: 'seq', width: 50 },
            { field: 'srcBillNo', title: '源票号', width: 120 },
            { field: 'srcTransTypeName', title: '源类型', width: 120 },
            { field: 'nowCheck', title: '本次核销', width: 120, formatter: 'formatMoney' ,align: 'right' },
            { field: 'yhCheck', title: '核销优惠', width: 120, formatter: 'formatMoney' ,align: 'right'},
            { field: 'remark', title: '备注'},
        ]
        invoice.columns = columns
        return ctx.suc('', invoice);
    }
    // 送货小票
    if (transType === 180001) {
        let list = await db.query(`select * from cargo where id=${billId}`, optionOne)
        if (!list) {
            return ctx.err('未找到送货小票数据');
        }
        return ctx.suc('', list);
    }
    // 凭证
    if (transType === 190001) {
        let sql = `select N'会计凭证' as transTypeName,a.id,a.billState,a.billNo,a.remark,b.name as buName,b.linkMobile,b.address,b.linkName,a.remark from card a left join contact b on a.buId=b.id where a.tenantId=${tenantId} and a.id=${billId}`;
        let invoice = await db.query(sql, option);
        if (invoice.length === 0) {
            return ctx.err('未找到凭证');
        }
        invoice = invoice[0];
        invoice.info = await db.query(`select * from card_info where cardId=${billId}`, option)
        if (invoice.billState === 'draft') {
            invoice.state = '草稿';
        }
        if (invoice.billState === 'accounted') {
            invoice.state = '已审核';
        }
        if (invoice.billState === 'del') {
            invoice.state = '已删除';
        }
        columns = [
            { type: 'seq', width: 50 },
            { field: 'remark', title: '摘要', width: 300 },
            { field: 'subjectName', title: '会计科目', width: 200 },
            { field: 'price1', title: '借方金额', width: 120, formatter: 'formatMoney' ,align: 'right' },
            { field: 'price2', title: '贷方金额', width: 120, formatter: 'formatMoney' ,align: 'right'}
        ]
        invoice.columns = columns
        return ctx.suc('', invoice);
    }

    return ctx.err('还未更新此类票据信息')
})



module.exports = router;
